package com.yinhai.message.manage.statistical.dao;

import com.yinhai.core.service.ta3.domain.dao.HibernateDAO;
import com.yinhai.message.core.po.MessageDetailPo;
import com.yinhai.message.manage.statistical.vo.MessageAnalysisVo;
import com.yinhai.sysframework.util.ValidateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.InformixDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.PostgresPlusDialect;
import org.hibernate.internal.SessionFactoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author:TANQINGPING
 * @version:1.0 2017/4/18
 * package:com.yinhai.message.manage.statistical
 * <p>Title: MessageAnalysisDaoImpl.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 四川久远银海软件股份有限公司</p>
 */

public class MessageAnalysisDaoImpl extends HibernateDAO<MessageDetailPo> implements IMessageAnalysisDao {
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<MessageAnalysisVo> getAnalysisInfo(String startDate, String endDate, String sendType) {
        Dialect dialect = ((SessionFactoryImpl)this.sessionFactory).getDialect();
        List<String> paramList = new ArrayList<>();
        StringBuffer str = new StringBuffer();
        StringBuffer gbaseStr = new StringBuffer();
        str.append("SELECT new com.yinhai.message.manage.statistical.vo.MessageAnalysisVo(to_char(a.senddate,'yyyy-mm-dd') as type,count(a.id.mgid) as count) FROM MessageDetailPo a WHERE 1=1");
        gbaseStr.append("SELECT to_char(a.senddate,'%Y-%m-%d') as type,count(a.mgid) as count  from tamessagedetail a where 1=1");
        boolean b = false;
        boolean e = false;
        boolean f = false;
        boolean g = false;
        if (!StringUtils.isEmpty(startDate)) {
            str.append(" and  a.senddate >= to_date(?,'yyyy-mm-dd HH24:MI:ss')");
            gbaseStr.append(" and  a.senddate >= to_date(?,'%Y-%m-%d %H:%M:%S')");
            paramList.add(startDate);
            b = true;
            e = true;
        }
        if (!StringUtils.isEmpty(endDate)) {
            str.append(" and  a.senddate <= to_date(?,'yyyy-mm-dd HH24:MI:ss')");
            gbaseStr.append(" and  a.senddate <= to_date(?,'%Y-%m-%d %H:%M:%S')");
            paramList.add(endDate);
            b = true;
            f = true;
        }
        if (!StringUtils.isEmpty(sendType)) {
            str.append(" and  a.id.sendertype = ?");
            gbaseStr.append(" and  a.sendertype = ?");
            paramList.add(sendType);
            b = true;
            g = true;
        }
        str.append(" GROUP BY to_char(a.senddate,'yyyy-mm-dd')");
        gbaseStr.append(" GROUP BY type");
        if (b) {
            if(dialect instanceof InformixDialect){
                List<MessageAnalysisVo> resultList = informaixHandle(gbaseStr, paramList);
                return  resultList;
            }else{
                return getMessageAnalysisVos(startDate, endDate, sendType, str, e, f, g);
            }
        }
        return super.selectFildsList(str.toString());
    }

    @Override
    public List<MessageAnalysisVo> getAnalysisInfoByH(String startDate, String endDate, String sendType) {
        StringBuffer str = new StringBuffer();
        StringBuffer gbaseStr = new StringBuffer();
        List paramList = new ArrayList();
        Dialect dialect = ((SessionFactoryImpl)this.sessionFactory).getDialect();
        str.append("SELECT new com.yinhai.message.manage.statistical.vo.MessageAnalysisVo(to_char(a.senddate,'yyyy-mm-dd HH24') as type,count(a.id.mgid) as count) FROM MessageDetailPo a WHERE 1=1");
        gbaseStr.append("SELECT to_char(a.senddate,'%Y-%m-%d %H') as type,count(a.mgid) as count FROM tamessagedetail a WHERE 1=1");

        boolean b = false;
        boolean e = false;
        boolean f = false;
        boolean g = false;
        if (!StringUtils.isEmpty(startDate)) {
            str.append(" and  a.senddate >= to_date(?,'yyyy-mm-dd HH24:MI:ss')");
            gbaseStr.append(" and  a.senddate >= to_date(?,'%Y-%m-%d %H:%M:%S')");
            paramList.add(startDate);
            b = true;
            e = true;
        }
        if (!StringUtils.isEmpty(endDate)) {
            str.append(" and  a.senddate <= to_date(?,'yyyy-mm-dd HH24:MI:ss')");
            gbaseStr.append(" and  a.senddate <= to_date(?,'%Y-%m-%d %H:%M:%S')");
            paramList.add(endDate);
            b = true;
            f = true;
        }
        if (!StringUtils.isEmpty(sendType)) {
            str.append(" and  a.id.sendertype = ?");
            b = true;
            g = true;
        }
        str.append(" GROUP BY to_char(a.senddate,'yyyy-mm-dd HH24')");
        gbaseStr.append(" GROUP BY type");
        if (b) {
            if(dialect instanceof InformixDialect){
                List<MessageAnalysisVo> resultList = informaixHandle(gbaseStr, paramList);
                return  resultList;
            }else{
                return getMessageAnalysisVos(startDate, endDate, sendType, str, e, f, g);
            }
        }
        return super.selectFildsList(str.toString());
    }

    @Override
    public List<MessageAnalysisVo> getTypesStatistic(String begin, String end, String sendType) {
        Dialect dialect = ((SessionFactoryImpl)this.sessionFactory).getDialect();
        StringBuffer str = new StringBuffer();
        str.append("SELECT new com.yinhai.message.manage.statistical.vo.MessageAnalysisVo(b.codeDESC as type,count(a.id.mgid) as count) " +
                "FROM MessageDetailPo a,com.yinhai.modules.codetable.domain.po.AA10Po b WHERE b.id.codeType='SENDMODE' and b.id.codeValue = a.id.sendertype");
        if(dialect instanceof OracleDialect){
            str.append(" and  a.senddate >= to_date(?,'yyyy-mm-dd HH24:MI:ss')");
            str.append(" and  a.senddate <= to_date(?,'yyyy-mm-dd HH24:MI:ss')");

        }else if(dialect instanceof PostgresPlusDialect){
            str.append(" and  a.senddate >= to_date(?,'yyyy-mm-dd HH24:MI:ss')");
            str.append(" and  a.senddate <= to_date(?,'yyyy-mm-dd HH24:MI:ss')");

        }else if(dialect instanceof InformixDialect){
            str.append(" and  a.senddate >= to_date(?,'%Y-%m-%d %H:%M:%S')");
            str.append(" and  a.senddate <= to_date(?,'%Y-%m-%d %H:%M:%S')");

        }
        if (!StringUtils.isEmpty(sendType)) {
            str.append(" and a.id.sendertype = ?");
        }
        str.append(" GROUP BY b.codeDESC");
        if (!StringUtils.isEmpty(sendType)) {
            return super.selectFildsList(str.toString(), begin, end, sendType);
        }
        return super.selectFildsList(str.toString(), begin, end);
    }

    private List<MessageAnalysisVo> getMessageAnalysisVos(String startDate, String endDate, String sendType, StringBuffer str, boolean e, boolean f, boolean g) {
        if (e && f && !g) {
            return super.selectFildsList(str.toString(), startDate, endDate);
        } else if (e && !f && g) {
            return super.selectFildsList(str.toString(), startDate, sendType);
        } else if (!e && f && g) {
            return super.selectFildsList(str.toString(), endDate, sendType);
        } else if (!e && !f && g) {
            return super.selectFildsList(str.toString(), sendType);
        } else if (!e && f && !g) {
            return super.selectFildsList(str.toString(), endDate);
        } else if (e && !f && !g) {
            return super.selectFildsList(str.toString(), startDate);
        }
        return super.selectFildsList(str.toString(), startDate, endDate, sendType);
    }

    private List<MessageAnalysisVo> informaixHandle(StringBuffer str,List paramList){
        SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(str.toString());
        for(int i = 0;i < paramList.size();i++){
            if(ValidateUtil.isNotEmpty(paramList.get(i)+"")){
                query.setParameter(i,paramList.get(i));
            }
        }
        List list = query.list();
        List<MessageAnalysisVo> resultList = new ArrayList<>();
        for (Object object:list){
            Object[] objects = (Object[]) object;
            MessageAnalysisVo messageAnalysisVo = new MessageAnalysisVo((String)objects[0],Long.parseLong(objects[1].toString()));
            resultList.add(messageAnalysisVo);
        }
        return resultList;
    }

}
